'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	let res1
	let {openid,nickName,avatarUrl} = event
	let res = await db.collection('uni-id-users').where({
		wx_openid: openid,
	}).count()
	if(res.total === 0){
		res1 = await db.collection('uni-id-users').add({
			nickname: nickName,
			avatar: avatarUrl,
			wx_openid: openid
		})
	}else{
		let res = await db.collection('uni-id-users').where({
			wx_openid: openid
		}).get()
		res1 = {
			...res.data[0]
		}
	}
	//返回数据给客户端
	return {
		code: 200,
		msg: '获取数据成功!',
		data: res1
	}
};
